# -*- conding:UTF-8 -*-

'''
@ Project: P8 flask_progect
@ File: comments.py
@ Author: 罗婷
@ Date：2022/6/24 11:33

'''


import json

from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, marshal
# from sqlalchemy import and_
from common.jwt.check_token import token_check
from common.model_fields.courses import *
from common.model_fields.users import users_fields
from common.models.course import *
from common.models.users import Users

comment_bp = Blueprint('comment_bp', __name__, url_prefix='/comment')

api = Api(comment_bp)

# 获取评论
class Comments_son(Resource):

    # course_id  课程id
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('course_id')
        args = parser.parse_args()
        course_id = args['course_id']
        # print('获取到的参数^^^^^^^^', 'course_id', course_id)

        # 一级评论
        comments = Comments.query.filter_by(course=course_id, parent_id=None).all()
        comments_data = json.loads(json.dumps(marshal(comments, comment_fields)))
        # print('一级评论```````````````', comments_data)

        for i in comments_data:
            # print('i2222222222', i)
            user = Users.query.filter_by(uid=i['user']).first()
            user_info = {
                'id': user.uid,
                'account': user.account,
                'phone': user.phone,
                'email': user.email,
                'profile_photo': user.profile_photo,
                'nick_name': user.nick_name,
            }
            i['user_info'] = user_info
            # 子评论
            com_son = Comments.query.filter_by(course=course_id, parent_id=i['id']).all()
            com_son_sum = Comments.query.filter_by(course=course_id, parent_id=i['id']).count()
            com_son_data = json.loads(json.dumps(marshal(com_son, comment_fields)))
            for j in com_son_data:
                user = Users.query.filter_by(uid=j['user']).first()
                user_info = {
                    'id': user.uid,
                    'account': user.account,
                    'phone': user.phone,
                    'email': user.email,
                    'profile_photo': user.profile_photo,
                    'nick_name': user.nick_name,
                }
                j['user_info'] = user_info
            i['childlist'] = com_son_data
            i['childlist_num'] = com_son_sum
        # print('评论嵌套comments_data```````````````', comments_data)
        return jsonify(message='评论信息获取成功', code=200, data=comments_data)

# 发表评论
class SendComments(Resource):
    def post(self):
        # content: marked(this.doc2),          评论内容
        # token: localStorage.getItem("token"),  token
        # ques_id: this.ques_id,   //       课程id
        # reply: id,               //      被评论的人的的id 没获取到就是对课程的评论
        parser = reqparse.RequestParser()
        argss = ['content', 'token', 'ques_id', 'reply']
        for i in argss:
            parser.add_argument(i)
        args = parser.parse_args()
        content = args['content']
        token = args['token']
        ques_id = args['ques_id']    # ques_id 0        ques_id 0  改为课程id
        reply = args['reply']        # 负id   评论reply None    回复reply 1
        print('获取到的参数^^^^^^^^', 'content', content, 'token', token,  'ques_id', ques_id,  'reply', reply)
        # 校验token
        payload = token_check(token)
        print('校验出的payloa@@@@@@@', payload)
        # {'uid': 1, 'account': 'admin1', 'exp': 1656336094}
        if payload:
            if not reply:   # 评论
                comms = Comments(user=payload['uid'], comment_content=content, parent_id=reply, course=ques_id)
                db.session.add(comms)
                db.session.commit()
                return jsonify(message='评论成功', code=200)
            else:
                comm = Comments(user=payload['uid'], comment_content=content, parent_id=reply, course=ques_id)
                db.session.add(comm)
                db.session.commit()
                return jsonify(message='回复成功', code=200)
        else:
            return jsonify(message='请登录后再评论', code=400)

api.add_resource(Comments_son, '/get_comment')
api.add_resource(SendComments, '/add_comment')






